Ext.define('Zm.controller.Lines', {
    extend: 'Ext.app.Controller',
    uses:['Zm.Common','Zm.i18n.Lang','Zm.view.line.Edit'],
	stores:['Line'],
    refs:[
        {ref:'X3dView', selector:'x3dview'},
        {ref:'List', selector:'linesearch'}
    ],
    init: function(){
        var me = this;
        me.control({
            'linesearch button[action=add]':{
                click:me.onAddLine
            },
            'linesearch button[action=del]':{
                click:me.onDelLine
            },
            'linesearch button[action=search]':{
                click:me.onSearch
            },
            'lineedit button[action=save]':{
                click:me.doAddLine
            }
        });
	},
    onAddLine:function(btn){
        var view = Ext.widget('lineedit');
    },
    doAddLine:function(btn){
        var me = this,
            win = btn.up('window'),
            frm = win.down('form'),
            form = frm.getForm();
        form.submit({
            url: g_app + '/Lines/save',
            scope: me,
            success: function(form,action){
                me.getLineStore().load();
                win.close();
            },
            failure: Zm.Common.onFail
        });
    },
    onDelLine:function(btn){
        var me = this,rd,
            lst =  me.getList(),
            sm = lst.getSelectionModel(),
            records = sm.getSelection(),
            lang = Zm.i18n.Lang;
        if(records.length>0){
            rd = records[0];
            Ext.MessageBox.show({
                   title:lang.tDel,
                   msg: Ext.String.format(lang.tDelLineMsg,rd.get('linename'),rd.get('devnum')),
                   buttons: Ext.Msg.YESNO,
                   fn: me.onDelCallback,
                   scope: me,
                   lineid:rd.get('lineid'),
                   icon: Ext.Msg.QUESTION
            });                                        
        }
    },
    onDelCallback: function(btn,text,opt){
        var me = this,
            lang = Zm.i18n.Lang,
            lineid = opt.lineid,
            cm = Zm.Common;
        if(btn == 'yes'){
            Ext.Ajax.request({
                url: g_app + '/Lines/delLine',
                params: {lineid:lineid},
                scope: me,
                success:function(response,options){ 
                    me.getLineStore().load();
                },
                failure:cm.onAFail
             });    
        }
    },
    onSearch:function(btn){
        var me = this,
            keyword = me.getList().down('#keyword').getValue(),
            store = me.getLineStore(),
            proxy = store.getProxy();
        proxy.extraParams = {keyword:keyword};
        store.load();
    }
});